﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Reflection;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class IMChatBox : Control
    {
        public StackPanel ListBox;
        public IMChatBox() {
            ListBox = new StackPanel()
            {
                Orientation = Orientation.Vertical,
                MarginTop = 0,
                MarginLeft = 0,
                MarginRight = 0,
                Children =
                {

                }
            };
        }
        ScrollViewer sv;
        //模板定义
        protected override void InitializeComponent()
        {
             sv = new ScrollViewer
            {
                MarginLeft = 0,
                MarginRight = 0,
                MarginTop = 0,
                MarginBottom = 0,
                Content = ListBox,
                
            };
            
            Children.Add(sv);
        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();

        }
        public void GotoItem(int index)
        {
            index -= 1;
            if (index > 0)
            {

                var item = ListBox.Children[index];
                float value= item.ActualOffset.Y + item.ActualSize.Height;
                sv.VerticalOffset = value;
            }
        }
        public void AddMessage(MessagesInfo messagesInfo)
        {
            ListBox.Children.Add(new IMChatBoxItem(messagesInfo)
            {
                MarginLeft = 0,
                MarginRight = 0,
            });
            GotoItem(ListBox.Children.Count - 1);
        }
        //用户代码

#endif
    }
}
